import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/process'

const api = {

    /**
     * 新增流程静态模板定义
     * @param process
     * @returns {Promise<*|undefined>}
     */
    createProcess: (process) =>
      axiosClient().post(`${requestUrl}/addInfo`, process),

    /**
     * 根据ID删除流程静态模板定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteProcessById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新流程静态模板定义
     * @param id
     * @param process
     * @returns {Promise<*|undefined>}
     */
    updateProcessFull: (id, process) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, process),

    /**
     * 根据ID部分更新流程静态模板定义
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateProcessPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询流程静态模板定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getProcessById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询流程静态模板定义
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getProcessPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询流程静态模板定义
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getProcessList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.templateCode) {
            params.append('templateCode', businessParam.templateCode)
        }
        if (businessParam.templateName) {
            params.append('templateName', businessParam.templateName)
        }
        if (businessParam.version) {
            params.append('version', businessParam.version)
        }
        if (businessParam.description) {
            params.append('description', businessParam.description)
        }
        if (businessParam.formId) {
            params.append('formId', businessParam.formId)
        }
        if (businessParam.processLoadFunc) {
            params.append('processLoadFunc', businessParam.processLoadFunc)
        }
        if (businessParam.submitFunc) {
            params.append('submitFunc', businessParam.submitFunc)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
